package com.isay.app.honey.ui.profit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.utils.DateUtils;
import com.isay.frameworklib.utils.ToastUtils;

import org.json.JSONObject;

import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.util.ConvertUtils;
import cn.qqtheme.framework.widget.WheelView;

/**
 * 我的收益
 */
public class MyProfitActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvProfit;
    private TextView mTvStartTime;
    private TextView mTvEndTime;

    private String mStartTime;
    private String mEndTime;

    public static void launch(Context context, String allProfit) {
        Intent intent = new Intent();
        intent.putExtra("allProfit", allProfit);
        intent.setClass(context, MyProfitActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_profit);
        initView();
        initData();
    }

    private void initData() {
        String allProfit = getIntent().getExtras().getString("allProfit");
        if (TextUtils.isEmpty(allProfit)) {
            getMyProfit("2000-1-16", "2099-1-16");
        } else {
            mTvProfit.setText(allProfit);
        }
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.white;
    }


    private void initView() {
        ((TextView) findViewById(R.id.tv_title)).setText("我的收益");
        findViewById(R.id.iv_go_back).setOnClickListener(this);
        mTvProfit = findViewById(R.id.tv_my_profit);
        mTvStartTime = findViewById(R.id.tv_start_time);
        mTvEndTime = findViewById(R.id.tv_end_time);
        findViewById(R.id.lay_start_time).setOnClickListener(this);
        findViewById(R.id.lay_end_time).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_go_back:
                finish();
                break;
            case R.id.lay_start_time:
                onYearMonthDayPicker(0);
                break;
            case R.id.lay_end_time:
                onYearMonthDayPicker(1);
                break;
            default:
                break;
        }
    }


    public void onYearMonthDayPicker(final int date) {
        String nyd = DateUtils.getLongDateToString(System.currentTimeMillis());
        int y = 2018;
        int m = 12;
        int d = 15;
        int maxY = 2111;
        int minY = 2016;
        if (!TextUtils.isEmpty(nyd)) {
            String[] arr = nyd.split("-");
            if (arr != null && arr.length == 3) {
                y = Integer.valueOf(arr[0]);
                if (y <= minY || y >= maxY) {
                    y = 2018;
                }
                m = Integer.valueOf(arr[1]);
                d = Integer.valueOf(arr[2]);
            }
        }
        final DatePicker picker = new DatePicker(this);
        picker.setCanceledOnTouchOutside(true);
        picker.setUseWeight(true);
        picker.setTopPadding(ConvertUtils.toPx(this, 10));
        picker.setRangeEnd(maxY, 12, 31);
        picker.setRangeStart(minY, 1, 1);
        picker.setSelectedItem(y, m, d);
        picker.setResetWhileWheel(false);
        //自定义
        int color = getResources().getColor(R.color.colorMain);
        picker.setTextColor(color); //2018 12 19
        picker.setLabelTextColor(WheelView.TEXT_COLOR_NORMAL);//年月日
        picker.setCancelVisible(false);
        // picker.setTextColor(0xffff0000);
        //  picker.setCancelTextColor(color);
        //  picker.setSubmitTextColor(color);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                if (date == 0) {
                    mStartTime = year + "-" + month + "-" + day;
                    mTvStartTime.setText(mStartTime + " >  ");
                } else {
                    mEndTime = year + "-" + month + "-" + day;
                    mTvEndTime.setText(mEndTime + " >  ");
                }
                getMyProfit(mStartTime, mEndTime);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }


    private void getMyProfit(String starttime, String endtime) {
        if (TextUtils.isEmpty(starttime) || TextUtils.isEmpty(endtime)) {
            return;
        }
        showLoading("查询中...");
        mTvProfit.setText("");
        NetworkManager.getMyProfit(starttime, endtime, new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    String money = jsonObject.optString("money");
                    if (money != null && money.length() > 0 && !money.contains(".")) {
                        money = money + ".00";
                    }
                    mTvProfit.setText(money);
                } catch (Exception e) {
                    onFailure(e);
                }
                if (ret != 200) {
                    ToastUtils.show(msg);
                }
                dismissDialog();
            }

            @Override
            public void onFailure(Exception e) {
                dismissDialog();
            }
        });
    }
}
